package com.yunao.service.report;

import org.ansj.app.keyword.KeyWordComputer;
import org.ansj.app.keyword.Keyword;
import org.apache.commons.lang3.Range;

import java.util.List;
import java.util.stream.Collectors;

public class KeywordUtils {
	
	public static String extract(String content) {
		int nKeyword = content.length() / 100;
		Range<Integer> range = Range.between(14, 20);
		nKeyword = range.contains(nKeyword) ? nKeyword
				: (range.getMinimum() > nKeyword ? range.getMinimum() : range.getMaximum());
		return extract(content, nKeyword).stream().map(Keyword::getName).collect(Collectors.joining("+"));
	}

	/**
	 * 提取一定数量的关键词
	 */
	public static List<Keyword> extract(String content, int nKeyword) {
		KeyWordComputer computer = new KeyWordComputer(nKeyword);
		return computer.computeArticleTfidf(content).stream()
				.map(ansjKeyword -> new Keyword(ansjKeyword.getName(), ansjKeyword.getFreq(), ansjKeyword.getScore()))
				.collect(Collectors.toList());
	}

}
